local iLvl = {
  Name = "Easy iLvl",
  Version = "v1.4.0",
  Author = "Ráncid@Firetree",
  frame = CreateFrame("Frame"),
  set = 0
  
}

local Items = {
  CharacterHeadSlot = 1,
  CharacterNeckSlot = 2,
  CharacterShoulderSlot = 3,
  CharacterBackSlot = 15,
  CharacterChestSlot = 5,
  CharacterWristSlot = 9,
  CharacterHandsSlot = 10,
  CharacterWaistSlot = 6,
  CharacterLegsSlot = 7,
  CharacterFeetSlot = 8,
  CharacterFinger0Slot = 11,
  CharacterFinger1Slot = 12,
  CharacterTrinket0Slot = 13,
  CharacterTrinket1Slot = 14,
  CharacterMainHandSlot = 16,
  CharacterSecondaryHandSlot = 17,
  CharacterRangedSlot = 18,
  CharacterAmmoSlot = 0
}

function iLvl_Update()
  local tCount = 0;
  local tSlots = 0;
  local uLvl = 0;

  for Key, Value in pairs(Items) do
    local ItemLink = GetInventoryItemLink("player", Value)
    local Slot = getglobal(Key.."Stock");
    
    if Slot then
      Slot:Hide();
      if ItemLink then
        local _, _, _, ItemLevel, _, _, _, StackCount = GetItemInfo(ItemLink)
  local cLvl, mLvl = iLvl_ItemUpgradeLevel(ItemLink)
  uLvl = 0;
  
  -- Incorporated nion91's changes
        if StackCount == 1 then
          if cLvl == nil then cLvl = 0 end
          if mLvl == nil then mLvl = 0 end
      
          uLvl = cLvl * 4
    
          --if mLvl > 0 and mLvl == cLvl then uLvl = 8 end
          --if mLvl == 2 and cLvl == 1 then uLvl = 4 end
          
          tCount = tCount + ItemLevel + uLvl;
          tSlots = tSlots + 1;
          Slot:SetText(ItemLevel + uLvl);
          
          if (cLvl / mLvl) > 0.75 then  -- 4/4 or 2/2 
            Slot:SetTextColor(0.3, 1, 0.3, 1); -- green
          elseif (cLvl / mLvl) > 0.25 then -- 1/2 or 2/4 or 3/4
            Slot:SetTextColor(1, 0.64, 0, 1); -- orange
          else  -- 0/0 or 0/2 or 0/4 or 1/4
            Slot:SetTextColor(1, 0.9, 0, 1); -- almost yellow
          end
          
          Slot:Show();
        end
      end
    end
  end
end


-- Code from Phanx (thank you)
local S_UPGRADE_LEVEL = "^" .. gsub(ITEM_UPGRADE_TOOLTIP_FORMAT, "%%d", "(%%d+)")

-- Create the tooltip:
local scantip = CreateFrame("GameTooltip", "iLvl_Tooltip", nil, "GameTooltipTemplate")
scantip:SetOwner(UIParent, "ANCHOR_NONE")

function iLvl_ItemUpgradeLevel(itemLink)
    -- Pass the item link to the tooltip:
    scantip:SetHyperlink(itemLink)

    -- Scan the tooltip:
    for i = 2, scantip:NumLines() do -- Line 1 is always the name so you can skip it.
      
        local text = _G["iLvl_TooltipTextLeft"..i]:GetText()
        if text and text ~= "" then
            local currentUpgradeLevel, maxUpgradeLevel = strmatch(text, S_UPGRADE_LEVEL)
            if currentUpgradeLevel then
                return tonumber(currentUpgradeLevel), tonumber(maxUpgradeLevel)
            end
        end
    end
end


iLvl.frame:SetScript("OnEvent", function(self, event, ...)
  if event == "PLAYER_ENTERING_WORLD" and iLvl.set == 0 then
    iLvl.set = 1;
    iLvl.frame:RegisterEvent("UNIT_PORTRAIT_UPDATE");
    iLvl.frame:RegisterEvent("ITEM_UPGRADE_MASTER_UPDATE");
  end
  
  if event == "UNIT_PORTRAIT_UPDATE" then
    iLvl_Update();
  end
end)

iLvl.frame:RegisterEvent("PLAYER_ENTERING_WORLD");